package com.cyz.springdemo.Controller;

import com.cyz.springdemo.comment.IMoocJSONResult;
import com.cyz.springdemo.comment.Resource;
import com.cyz.springdemo.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: springdemo
 * @description: 项目首次启动类
 * @author: wangjie
 * @create: 2019-07-23 19:43
 **/
@RestController             //@RestController = @Controller + @ResponseBody
@RequestMapping(value = "/hello")
public class HelloController {

    private Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private Resource resource;
    /*@Autowired
    private RedisTemplate<String,String> redisTemplate;*/

    @Autowired
    private RedisTemplate<String,User> redisTemplate;

    //设置一个全局的自增变量
    private static AtomicInteger atomicInteger = new AtomicInteger(1);

    /**
     * 测试HelloFilter过滤器
     * @param request
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public String hello(HttpServletRequest request){
        String name = request.getParameter("name");
        return "Hello SpringBoot" + name;
    }

    /**
     * 测试生产消费队列
     * @param name
     * @return
     */
    @RequestMapping(value = "/getUserJson",method = RequestMethod.GET)
    public IMoocJSONResult getUserJson(String name){

        for (int i=0;i<10;i++){
            User u = new User();
            u.setName(UUID.randomUUID().toString());
            u.setAga(atomicInteger.getAndIncrement());     //自动加1
            u.setBirthday(new Date());
            u.setPassword("123456");
            //u.setDesc(redisTemplate.opsForValue().get("aa"));

            redisTemplate.opsForList().rightPush("my:list",u);
        }

        return IMoocJSONResult.ok();
    }

    /**
     * 测试资源文件注入
     * @return
     */
    @RequestMapping(value = "/getResourse")
    public IMoocJSONResult getResourse(){

        Resource bean = new Resource();
        BeanUtils.copyProperties(resource,bean);

        return IMoocJSONResult.ok(bean);
    }

    /**
     * 测试redis缓存获取
     * @return
     */
    @RequestMapping(value = "/redis",method = RequestMethod.GET)
    public String redisTest(){

        //redisTemplate.opsForValue().set("aa1","redis123");

        return "Hello Redis";
    }

    @RequestMapping(value = "/week",method = RequestMethod.GET)
    public Integer getWeekOfYear() throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //Date date = format.parse(DateFormatUtils.format(new Date(),"yyyy-MM-dd"));
        Date date = format.parse("2019-08-18");
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));

        int a  = calendar.get(Calendar.WEEK_OF_YEAR);

        return a;
    }

}
